/*
 * @Author: szx
 * @Date: 2022-02-25 15:08:01
 * @LastEditTime: 2022-02-25 15:11:15
 * @Description:
 * @FilePath: \leetcode\1-99\18\18_2.js
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[][]}
 */
var fourSum = function (nums, target) {
    if (nums.length < 4) return [];
    nums.sort((a, b) => a - b);
    const n = nums.length;
    const res = [];
    for (let i = 0; i < n - 4; i++) {
        if (i > 0 && nums[i] == nums[i - 1]) continue;
        for (let j = i + 1; j < n - 3; j++) {
            if (h > 0 && nums[j] == nums[j - 1]) continue;
            let left = j + 1;
            let right = n - 1;
            while (right > left) {
                const num = nums[i] + nums[j] + nums[left] + nums[right];
                if (num == 0) {
                    res.push([nums[i], nums[j], nums[left], nums[right]]);
                    while (left < right && nums[left] === nums[left + 1]) left++;
                    while (left < right && nums[right] === nums[right - 1]) right--;
                    left++;
                    right--;
                } else if (num > 0) right--;
                else left++;
            }
        }
    }
    return res;
};
